package com.ys.service.sys.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.sys.TbCompanyInfo;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.sys.TbCompanyInfoMapper;
import com.ys.service.sys.TbCompanyInfoService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("tbCompanyInfoService")
public class TbCompanyInfoImpl implements TbCompanyInfoService {

    @Autowired
    private TbCompanyInfoMapper tbCompanyInfoMapper;

    @Autowired
    private CheckUtil checkUtil;



    /**
     * 修改公司信息
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TbCompanyInfo tbCompanyInfo = JSON.parseObject(JSON.toJSONString(params), TbCompanyInfo.class);

        tbCompanyInfoMapper.update(tbCompanyInfo);
        return R.ok(AddDto.gotoEdit("/web/sys/tbCompanyInfo/toEdit?id=" + tbCompanyInfo.getId()));

    }

    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+10245L)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TB_CompanyInfo",
                    listField,
                    infoMap,
                  null
            );
            if(infoMap.get("idNumber")!=null && !checkUtil.isIDNumberValid(infoMap.get("idNumber").toString())){
                backMsg = "法人身份证号格式有误！";
            }
            if(infoMap.get("phone")!=null && !checkUtil.isPhone(infoMap.get("phone").toString())){
                backMsg = "法人联系电话格式有误！";
            }
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
